(ns ca.uoit.molly.main.build-indexes
  (:require [ca.uoit.molly.db-molly :as db-molly]
            [ca.uoit.molly.lucene-helper :as lucene]
            [ca.uoit.molly.lucene-molly :as lucene-molly]
            [ca.uoit.molly.mycampus :as mycampus]))

(defn build-entity-index [& args]
  "Builds entity indexe"
  (let [analyzer (lucene/mk-analyzer)
        writer (lucene/mk-index-writer "mycampus-entity.idx" analyzer)
        index (fn [ent]
                (. writer addDocument (lucene-molly/entity-to-doc ent)))
       ]
    (doseq [[name ent-def] mycampus/*entities*]
      (print "Indexing " name " ")
      (time (db-molly/for-all-entities
        mycampus/*db*
        ent-def
        index)))
    (lucene/close-index-writer writer)))

(defn build-value-index [& args]
  "Builds value index"
  (let [analyzer (lucene/mk-analyzer)
        writer (lucene/mk-index-writer "mycampus-value.idx" analyzer)
        index (fn [v]
                (let [doc (lucene-molly/value-to-doc v)]
                  (. writer addDocument doc)))
       ]
    (doseq [[name ent-def] mycampus/*entities*]
      (print "Value indexing " name " ")
      (time (db-molly/for-all-values
              mycampus/*db*
              ent-def
              index)))
    (lucene/close-index-writer writer)))

(def *counter* (ref {:n 0 :t (System/currentTimeMillis)}))
(defn- inc-counter
  [c]
  (if (zero? (mod (:n c) 100))
    (do 
      (let [dt (- (System/currentTimeMillis) (:t c))
            n (:n c)
            rate (* (/ 100.0 dt) 1000)]
          (println (format "Processed %d @ %.2f groups / second." n rate)))
      (assoc c :t (System/currentTimeMillis) :n (inc (:n c))))
    (assoc c :n (inc (:n c)))))

(defn build-group-index 
  []
  (let [analyzer (lucene/mk-analyzer)
        writer (lucene/mk-index-writer "mycampus-group.idx" analyzer)
        index (fn [grp-def denorm-grp]
                (do 
                  (dosync (alter *counter* inc-counter))
                  (. writer addDocument 
                    (lucene-molly/denorm-group-to-doc grp-def denorm-grp))))]
    (time (db-molly/for-all-groups
            mycampus/*db*
            mycampus/*course-schedule*
            index))
    (lucene/close-index-writer writer)))

(defn main [& args]
  (do
    (build-entity-index)
    (build-value-index)
    (build-group-index)))

(main)


